﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Nivlag.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {

        public string anystring;

 
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void ReceivedField_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
        }

        public void SetReceivedField (string theValue)
        {
            ReceivedField.Text = theValue;

            this.XmlSharePointData.Data = theValue;

            TreeNodeBinding tnb = new TreeNodeBinding();
            tnb.DataMember = "web"; // This is the label in the xml for a site.
            tnb.TargetField = "title";
            tnb.ValueField = "title";
            tnb.ToolTipField = "Template";

            this.SharePointSiteHierarchyTreeView.DataBindings.Add(tnb);

            tnb = new TreeNodeBinding();
            tnb.DataMember = "list"; // This is the label in the xml for a site.
            //tnb.TargetField = "listid";
            tnb.ValueField = "title";
            tnb.ToolTipField = "Template";
            
            
            this.SharePointSiteHierarchyTreeView.DataBindings.Add(tnb);

            this.SharePointSiteHierarchyTreeView.DataSourceID = "XmlSharePointData";
            SharePointSiteHierarchyTreeView.Visible = true;

        } // SetReceiveField

        protected void TreeView1_SelectedNodeChanged1(object sender, EventArgs e)
        {

            WebListUrlParameters wlup = null;

            try
            {
                this.DebugTextField.Text = "Node was clicked, value: [" + SharePointSiteHierarchyTreeView.SelectedNode.Value + "].";
                this.DebugTextField.Text += "///" + SharePointSiteHierarchyTreeView.SelectedNode.Target;

                // SelectedNode.Target = something like: http://demo2010a:9090/helloworldsandbox[delim]200c735a-1a52-4392-948b-619e87226f9d 

                try
                {
                    wlup = new WebListUrlParameters(SharePointSiteHierarchyTreeView.SelectedNode.Target);
                    this.DebugTextField.Text += wlup.ListIdAsString + "!!!" + wlup.WebUrl + "!!!";

                    ListAnalysis la = new ListAnalysis(wlup);

                    List<string> LibProperties = new List<string>();
                    LibProperties = la.AnalyzeLibrary();

                    this.LibAnalysisLabel.Text = "Start parsing LibProperties.";

                    foreach (string s in LibProperties)
                    {
                        this.LibAnalysisLabel.Text += "///" + s;
                    }
                }
                catch (Exception wlupE)
                {
                    this.DebugTextField.Text += "!!!exception parsing param!!!";
                }


                SharePointSiteHierarchyTreeView.ExpandDepth = SharePointSiteHierarchyTreeView.SelectedNode.Depth;
            }
            catch (Exception e2)
            { }
            
        }
    }
}
